package components.resultLayer
{
	import UIFree.ObjectEvent;
	import UIFree.uif_bitmap;
	
	import classes.clt_events;
	
	import data.cd_utils;
	
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class clt_elemBitmap extends uif_bitmap {
		public var colorCorrect: ColorTransform;
		public function clt_elemBitmap(a_elemName: String, bitmapData: BitmapData, a_data: Object) {
			super(bitmapData, 'auto', true);
			name = a_elemName;
			_data = a_data;
		}

		public function get isNoEars(): Boolean {
			return !_data || !_data.ears;
		}
		
		protected function calcElemMatrix(maskInfo: Object, a_faceRect: Rectangle, scale: Number, isOffset: Boolean=true): Matrix {
			
			function calcMatrix(ears1: Array, ears2: Array): Matrix {
				var scale	: Number = ears1[1].subtract(ears1[0]).length/ears2[1].subtract(ears2[0]).length;
				var mat		: Matrix = new Matrix();
				mat.translate(-ears2[0].x, -ears2[0].y);
				mat.scale(scale, scale);
				mat.translate(ears1[0].x, ears1[0].y);
				return mat;
			}
			
			if (!isNoEars) {
				var me: Array = cd_utils.getEars(maskInfo);
				var he: Array = cd_utils.getEars(_data);
				
				var h_mat: Matrix = calcMatrix(me, he);
				if (isOffset && maskInfo[name]) 
					h_mat.translate(maskInfo[name][0], maskInfo[name][1]);
				h_mat.scale(scale, scale);
				h_mat.translate(a_faceRect.x, a_faceRect.y);
				return h_mat;
			} else return new Matrix();
		}
		
		public function updateTransform(rb: clt_resultBitmap, offset: Point=null, scale: Number=1): void {
			var me			: Array 	= cd_utils.getEars(rb.maskInfo);
			var h_mat		: Matrix 	= rb.faceMatrixCalc(rb.bitmapData);
			var _faceRect	: Rectangle = rb.getFaceRect();
			var e_mat		: Matrix 	= getMatrix(rb.maskInfo, _faceRect, rb.scaleFromMask(bitmapData), false);
			var correct		: Matrix	= new Matrix();
			var _scaleM		: Number	= rb.scaleFromMask(bitmapData);
			e_mat.invert();
			
			if (offset) correct.translate(offset.x, offset.y);
			
			correct.concat(e_mat);
			h_mat.concat(correct);
			
			h_mat.translate(-(x + width) / 2, -(y + height) / 2);
			h_mat.scale(scale, scale);
			h_mat.translate((x + width) / 2, (y + height) / 2);
			
			me[0] = h_mat.transformPoint(me[0]);
			me[1] = h_mat.transformPoint(me[1]);
			var ears: Array = [
				Math.round(me[0].x),
				Math.round(me[0].y),
				Math.round(me[1].x),
				Math.round(me[1].y)
			]
			
			_data.ears = ears;
			
			rb.traceUpdateElement(_data);
		}
		
		public function getMatrix(maskInfo: Object, a_faceRect: Rectangle, scale: Number, isOffset: Boolean=true): Matrix {
			return calcElemMatrix(maskInfo, a_faceRect, scale, isOffset);
		}
		
		public function drawAsScale(bmd: BitmapData, scale: Number): void {
			var matrix: Matrix = transform.matrix.clone();
			matrix.scale(scale, scale);
			bmd.draw(this, matrix, colorCorrect);
		}
		
		public function draw(bmd: BitmapData, facePercent: Number, 
							 maskInfo: Object, a_faceRect: Rectangle): void {
			bmd.draw(this, getMatrix(maskInfo, a_faceRect, 
				(bmd.width * facePercent) / (maskInfo.ears[2] - maskInfo.ears[0])), colorCorrect);
		} 
	}
}